<?php
/**
 * This file is part of Swoft.
 *
 * @link https://swoft.org
 * @document https://doc.swoft.org
 * @contact group@swoft.org
 * @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Controllers\Admin;

use App\Models\Logic\IndexLogic;
use Swoft\App;
use Swoft\Bean\Annotation\Inject;
use Swoft\Core\Application;
use Swoft\Core\Coroutine;
use Swoft\Http\Message\Server\Request;
use Swoft\Http\Server\Bean\Annotation\Controller;
use Swoft\Http\Server\Bean\Annotation\RequestMapping;
use Swoft\Http\Server\Bean\Annotation\RequestMethod;
use Swoft\Task\Task;
use Swoft\View\Bean\Annotation\View;

/**
 * 控制器demo.
 *
 * @Controller(prefix="/tool")
 */
class ToolController
{
    /**
     * 别名注入.
     *
     * @Inject("httpRouter")
     *
     * @var \Swoft\Http\Server\Router\HandlerMapping
     */
    private $router;

    /**
     * 别名注入.
     *
     * @Inject("application")
     *
     * @var Application
     */
    private $application;

    /**
     * 注入逻辑层
     *
     * @Inject()
     *
     * @var IndexLogic
     */
    private $logic;


    /**
     * 视图渲染demo - 没有使用布局文件.
     *
     * @RequestMapping(route="/tool/formatJsonToMarkdown", method=RequestMethod::GET)
     * @View(template="tool/formatJsonToMarkdown", layout="layouts/my.php")
     */
    public function view()
    {
        return [];
    }

    /**
     *
     * @RequestMapping(route="/tool/format", method={RequestMethod::POST})
     *
     * @param Request $request
     *
     * @return array
     */
    public function format(Request $request)
    {
        $content = $request->post('content');

        $res = [];
        if ($content) {
            $contentArr = json_decode($content, true);
            if ($contentArr && is_array($contentArr)) {

                $mdData = $this->getMdData($contentArr);
                $mdTb = $this->formatMd($mdData);

                $res['status'] = 200;
                $res['data'] = $mdTb;
            } else {
                $res['msg'] = 'json结构不正确';
            }
        } else {
            $res['msg'] = "请传值";
        }
        return $res;
    }

    /**
     * @param $data array
     * @return string
     */
    private function formatMd($data)
    {
        $str = '';

        foreach ($data as $k => $v) {
            $str .= $v['key'] . " |  | " . gettype($v['value']) . "\n";
        }


        return $str;
    }

    /**
     * 获取json结构
     *
     * @param $arr
     * @param string $prefix
     * @return array
     */
    private function getMdData($arr, $prefix = '')
    {

        $res = [];
        foreach ($arr as $k => $v) {
            if (is_string($k) || is_int($k) && $k == 0) {
                if (!is_array($v)) {
                    $one['key'] = $prefix ? $prefix. "." . $k : $k;
                    $one['value'] = $v;

                    $res[] = $one;
                } else {
                    $one['key'] = $prefix ? $prefix. "." . $k : $k;
                    $one['value'] = null;
                    $res[] = $one;

                    $newPrefix = is_int($k) ? $prefix : ($prefix ? $prefix. "." . $k : $k);
                    $res = array_merge($res, $this->getMdData($v, $newPrefix));
                }
            }
        }
        return $res;
    }
}
